<?php
class MerchantSearch extends CFormModel {
	public $keyword;
	public $keywordRange;
	public $address;

	public static function getKeywordRanges() {
		return array(
			'1' => '名称及别名',
			'2' => '经营范围',
			'3' => '老板姓名',
			'4' => '联系人姓名',
			'5' => '备注'
		);
	}

	public function rules() {
		return array(
			array(
				'keyword, address',
				'length',
				'max' => 255
			),
			array(
				'keywordRange',
				'type',
				'type' => 'array'
			),
		);
	}

	public function attributeLabels() {
		return array(
			'address' => '地址',
			'keyword' => '关键词',
		);
	}

	/**
	 * @return CDbCriteria
	 */
	public function createCriteria() {
		$criteria = new CDbCriteria;
		if ($this->keyword) {
			$keywordCriteria = new CDbCriteria();
			foreach ($this->keywordRange as $range) {
				switch ($range) {
				case '1':
					$keywordCriteria->addSearchCondition('name', $this->keyword, true, 'OR');
					$keywordCriteria->addSearchCondition('otherName', $this->keyword, true, 'OR');
					break;
				case '2':
					$keywordCriteria
						->addSearchCondition('serviceScope', $this->keyword, true, 'OR');
					break;
				case '3':
					$keywordCriteria->addSearchCondition('bossName', $this->keyword, true, 'OR');
					break;
				case '4':
					$keywordCriteria->addSearchCondition('contactName', $this->keyword, true, 'OR');
					break;
				case '5':
					$keywordCriteria->addSearchCondition('note', $this->keyword, true, 'OR');
					break;
				default:
					break;
				}
			}
			$criteria->mergeWith($keywordCriteria);
		}

		if ($this->address) {
			$criteria->addSearchCondition('address', $this->address, true);
		}

		$criteria->order = 'name ASC';

		return $criteria;
	}
}
